package chances.system.operator.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Proxy;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@Table(name = "CS_SYS_OPERATOR")
@Proxy(lazy = false)
public class Operator implements UserDetails,Serializable {

	private static final long serialVersionUID = -5885508721423662537L;

	public static final String PATH_SEPARATOR = "#";

	public static final int STATUS_NORMAL = 0;
	public static final int STATUS_DISABLED = -1;
	public static final int STATUS_DELETED = -2;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "op_id")
	private Long id;

	@Column(name = "name", length = 32)
	private String name;

	@Column(name = "login_name", length = 64, nullable = false)
	private String loginName;

	@Column(name = "password", length = 64)
	private String password;

	@JsonIgnore
	@Column(name = "SALT", length = 64)
	private String salt;

	@Column(name = "status", length = 2)
	private int status;

	@Column(name = "email", length = 128)
	private String email;

	@Column(name = "mobile", length = 11)
	private String mobile;

	@JsonIgnore
	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "CS_SYS_OPERATOR_ROLE", joinColumns = { @JoinColumn(name = "op_id") }, inverseJoinColumns = {
			@JoinColumn(name = "role_id") })
	private Set<Role> roles = new LinkedHashSet<Role>();

	@Column(name = "org_id")
	private Long orgId;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the loginName
	 */
	public String getLoginName() {
		return loginName;
	}

	/**
	 * @param loginName
	 *            the loginName to set
	 */
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	/**
	 * @return the status
	 */
	public int getStatus() {
		return status;
	}

	/**
	 * @param status
	 *            the status to set
	 */
	public void setStatus(int status) {
		this.status = status;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the mobile
	 */
	public String getMobile() {
		return mobile;
	}

	/**
	 * @param mobile
	 *            the mobile to set
	 */
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	/**
	 * 操作号账号是否过期.
	 * 
	 * @return boolean
	 */
	public boolean isAccountNonExpired() {
		return true;
	}

	/**
	 * 操作号账号是否被锁.
	 * 
	 * @return boolean
	 */
	public boolean isAccountNonLocked() {
		return true;
	}

	/**
	 * 操作号密码是否过期(目前用不到).
	 * 
	 * @return boolean
	 */
	public boolean isCredentialsNonExpired() {
		return true;
	}

	/**
	 * 操作员状态是否正常.
	 * 
	 * @return boolean
	 */
	public boolean isEnabled() {
		if (status == STATUS_NORMAL) {
			return true;
		} else {
			return false;
		}
	}

	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}


	public String getSalt() {
		return salt;
	}

	public void setSalt(String salt) {
		this.salt = salt;
	}

	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	public Long getOrgId() {
		return orgId;
	}

	public void setOrgId(Long orgId) {
		this.orgId = orgId;
	}
	
	public List<Long> getRoleIds() {
		List<Long> ids = this.roles.stream().map(Role::getId).collect(Collectors.toList());
		return ids;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();  
        GrantedAuthority au = new SimpleGrantedAuthority("ROLE_USER");  
        list.add(au);  
        return list; 
	}

	@Override
	public String getUsername() {
		return loginName;
	}
}
